<?php

	define('IN_NWIDC', true);
	require_once('global.php');
	require_once(dirname(__FILE__) . '/core/nwidc.php');


	$send_goods_url = 'http://60.191.39.196:8001/ecm/interface/rest/SPWLInterface/sendCommodity';
	$send_order_url = 'http://60.191.39.196:8001/ecm/interface/rest/SPWLInterface/pushSaleOrder';

	$company_code = 15098765;

	//处理未备案的商品
	$sql = "select * from ra_goods_common where goods_verify = 10";
	$goods = getAll($sql);
	$json = array('Commoditys' => array());
	$ready_goods = array();
	for($i = 0; $i < 500 && $i < count($goods); $i++){
		$item = $goods[$i];
		if($item['open_tax'] != 1){
			continue;
		}
		$data = array(
			'goods_common' => $item,
			'goods_extend' => getRow("select * from ra_custom_hangzhou_goods where goods_commonid = $item[goods_commonid]"),
			'goods' => getRow("select * from ra_goods_common where goods_commonid = $item[goods_commonid]")
		);
		$ready_goods[] = $item['goods_commonid'];
		$str = (string)$i;
		while(strlen($str) < 4){
			$str  = '0' . $str;
		}

		$json['Commoditys'][] = array(
			"commodityCode" =>  $company_code . $str,
			"commodityName" => $data['goods']['goods_name'],
			'commoditySpec' => isset($data['goods_extend']['commoditySpec']) ? $data['goods_extend']['commoditySpec'] : '',
			'commodityArtNo' => isset($data['goods_extend']['commodityArtNo']) ? $data['goods_extend']['commodityArtNo'] : '',
			'commodityMadeTime' => isset($data['goods_extend']['commodityMadeTime']) ? $data['goods_extend']['commodityMadeTime'] : '',
			'durabilityPeriod' => isset($data['goods_extend']['durabilityPeriod']) ? $data['goods_extend']['durabilityPeriod'] : '',
			'brandId' => isset($data['goods']['brandId']) ? $data['goods']['brandId'] : '',
			'price' => isset($data['goods_common']['goods_costprice']) ? $data['goods_common']['goods_costprice'] : '',
			'marketPrice' => isset($data['goods_common']['goods_marketprice']) ? $data['goods_common']['goods_marketprice'] : '',
			'serialcontrol ' => isset($data['goods_extend']['serialcontrol ']) ? $data['goods_extend']['serialcontrol '] : '',
			'typeId' => isset($data['goods_extend']['typeId']) ? $data['goods_extend']['typeId'] : '',
			'unit' => isset($data['goods_extend']['unit']) ? $data['goods_extend']['unit'] : '',
			'weight' => isset($data['goods_extend']['weight']) ? $data['goods_extend']['weight'] : '',
			'boxSpec' => isset($data['goods_extend']['boxSpec']) ? $data['goods_extend']['boxSpec'] : '',
			'remark' => isset($data['goods_extend']['remark']) ? $data['goods_extend']['remark'] : '',
			'madeAddr' => isset($data['goods_extend']['madeAddr']) ? $data['goods_extend']['madeAddr'] : '',
			'tradeCountryName' => isset($data['goods_extend']['tradeCountryName']) ? $data['goods_extend']['tradeCountryName'] : '',
			'tradeCountryCode ' => isset($data['goods_extend']['tradeCountryCode ']) ? $data['goods_extend']['tradeCountryCode '] : '',
			'HScode' => isset($data['goods_extend']['HScode']) ? $data['goods_extend']['HScode'] : '',
		);
	}

	$result = send_post($send_goods_url,$json,function($result){
		//全部失败
		if($result['resultCode'] == 1005){
			return;
		}
		if($result['resultCode'] == 1025){
			$arr = array_map(function($item){
				return $item
			},$result['ERROR']);
			foreach($result['ERROR'] as $val){

			}
		}
	});

	print_r($result);
	// exit;


	function send_post($url,$json,$callback = null){
		$param = array();
		$param['v'] = '1.0';
		$param['ip'] = '127.0.0.1';
		$param['sessionKey'] = '2015SPWLAV1';
		$param['datetime'] = date('YmdHis');
		$param['appKey'] = 'SPWL';
		$sign = implode($param);
		$sign = strtoupper(md5(($sign)));
		$param['sign'] = $sign;
		$param['JSON_OBJ'] = base64_encode(json_encode($json));
		$result = file_get_contents_post($url,$param);

		if($callback){
			$json = json_decode($result);
			$callback($json['ROWSET']);
		}

		return $result;
	}

	function file_get_contents_post($url, $post) {  
	    $options = array(  
	        'http' => array(  
	            'method' => 'POST',  
	            // 'content' => 'name=caiknife&email=caiknife@gmail.com',  
	            'content' => http_build_query($post),  
	        ),  
	    );  
	  
	    $result = file_get_contents($url, false, stream_context_create($options));  
	  
	    return $result;  
	}  
	//$goods = Model()->table('goods')->where(array('goods_verify' => 10))->select();
	// file_put_contents('lot.txt', 123456),
?>